/*
 * SisAP - Sistema de Alocacao de Professores
 *
 * Copyright (C) 2011  Macambira Team.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.googlecode.conection;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * Classe responsavel por abrir a conecção com o banco de dados
 *
 * @version 0.1 03 dec 2011
 * @author Jafet
 */
public class ConectaDB {
    public static ConectaDB conect = null;
    private Connection con = null;

    public ConectaDB() throws ClassNotFoundException {
        this.criarConexao();
    }

    public static ConectaDB getInstance() throws ClassNotFoundException {        
            conect = new ConectaDB();        
        return conect;
    }

    /**
     * Este metodo cria uma conexao com o banco de dados.
     * @throws ClassNotFoundException
     */
    public void criarConexao() throws ClassNotFoundException {
        FileInputStream file = null;
        try {
            file = new FileInputStream("isisap.properties");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ConectaDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        Properties conexao = new Properties();
        try {
            conexao.load(file);
        } catch (IOException ex) {
            Logger.getLogger(ConectaDB.class.getName()).log(Level.SEVERE, null, ex);
        }

        String driver = conexao.getProperty("driver");
        String path = conexao.getProperty("path");
        String login = conexao.getProperty("login");
        String senha = conexao.getProperty("senha");

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(path, login, senha);
            System.out.println("Seu projeto foi conectado ao Mysql com sucesso.");
        } catch (SQLException sqlerror) {
            System.out.println("A conexao nao foi bem sucedida " + sqlerror);
        } catch (ClassNotFoundException Driver) {
            System.out.println("O driver nao foi encontrado " + Driver);
        }

    }

    /**
     * Este metodo fecha a conexao com o banco de dados.
     * @throws SQLException
     */
    public void fecharConexao() throws SQLException {
        this.con.close();
    }

    /**
     * metodo de conexao
     * @return a conexao
     */
    public Connection getCon() {
        return con;
    }

}


